package com.xxx.video.commen.utils;

import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import java.lang.reflect.Field;

public class ViewPager2Utils {
    private static ViewPager2Utils mViewPager2Utils = null;

    private ViewPager2Utils() {
    }

    public static ViewPager2Utils getInstance() {
        if (mViewPager2Utils == null) {
            synchronized (ViewPager2Utils.class) {
                if (mViewPager2Utils == null) {
                    mViewPager2Utils = new ViewPager2Utils();
                }
            }
        }
        return mViewPager2Utils;
    }

    public void reduceDragSensitivity(ViewPager2 viewPager2) {
        try {
            Field ff = ViewPager2.class.getDeclaredField("mRecyclerView");
            ff.setAccessible(true);
            RecyclerView recyclerView = (RecyclerView) ff.get(viewPager2);
            Field touchSlopField = RecyclerView.class.getDeclaredField("mTouchSlop");
            touchSlopField.setAccessible(true);
            int touchSlop = (int) touchSlopField.get(recyclerView);
            touchSlopField.set(recyclerView, touchSlop * 4);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
